{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "待修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "zero = 0.0001\n",
    "\n",
    "class Point(): #默认继承自系统的object类\n",
    "    '''三维点的类\n",
    "    '''\n",
    "    count = 0 #静态成员变量\n",
    "    \n",
    "    def __init__(self, x=0, y=0, z=0):\n",
    "        '''构造函数\n",
    "        '''\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        self.z = z\n",
    "        Point.count += 1\n",
    "    \n",
    "    def __del__(self):\n",
    "        '''析构函数\n",
    "        '''\n",
    "        Point.count -= 1\n",
    "    \n",
    "    @staticmethod\n",
    "    def getNumber(cls):\n",
    "        '''静态方法\n",
    "        '''\n",
    "        return Point.count\n",
    "    \n",
    "    @classmethod\n",
    "    def getNumber2(cls):\n",
    "        '''类方法\n",
    "        '''\n",
    "        return Point.count\n",
    "\n",
    "    #属性\n",
    "    def __getattr__(self, name):\n",
    "        try:\n",
    "            return self.__dict__[name]\n",
    "        except:\n",
    "            print('没有该属性')\n",
    "            return None\n",
    "    \n",
    "    def __setattr__(self, name, value):\n",
    "        self.__dict__[name] = value\n",
    "    \n",
    "    def __delattr__(self, name):\n",
    "        del self.__dict__[name]\n",
    "\n",
    "#    def __getattribute__(self, name):\n",
    "#        return self.__dict__[name]\n",
    "    \n",
    "    def __contains__(self, value):\n",
    "        '''in\n",
    "        '''\n",
    "        return self.x==value or self.y==value or self.z==value\n",
    "\n",
    "    #转换\n",
    "    def __repr__(self):\n",
    "        return str(self.x) + '\\t' + str(self.y) + '\\t' + str(self.z)\n",
    "    \n",
    "    def __int__(self):\n",
    "        return Point(int(self.x), int(self.y), int(self.z))\n",
    "\n",
    "    #比较\n",
    "    def __eq__(self, p):\n",
    "        '''==\n",
    "        '''\n",
    "        return math.sqrt((self.x-p.x)*(self.x-p.x) + (self.y-p.y)*(self.y-p.y)\\\n",
    "                         + (self.z-p.z)*(self.z-p.z)) < zero\n",
    "    \n",
    "    def __cmp__(self, other):\n",
    "        if self.x < other.x:\n",
    "            return -1\n",
    "        elif self.x > other.x:\n",
    "            return 1\n",
    "        elif self.y < other.y:\n",
    "            return -1\n",
    "        elif self.y > other.y:\n",
    "            return 1\n",
    "        elif self.z < other.z:\n",
    "            return -1\n",
    "        elif self.z > other.z:\n",
    "            return 1\n",
    "        else:\n",
    "            return 0\n",
    "    \n",
    "    def __lt__(self, other):\n",
    "        '''<\n",
    "        '''\n",
    "        if self.x < other.x:\n",
    "            return True\n",
    "        elif self.x > other.z:\n",
    "            return False\n",
    "        elif self.y < other.y:\n",
    "            return True\n",
    "        elif self.y > other.y:\n",
    "            return False\n",
    "        elif self.z < other.z:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "    \n",
    "    def __gt__(self, other):\n",
    "        '''>\n",
    "        '''\n",
    "        pass\n",
    "\n",
    "    #运算\n",
    "    def __add__(self, other):\n",
    "        return Point(self.x+other.x, self.y+other.y, self.z+other.z)\n",
    "    \n",
    "    def __sub__(self, other):\n",
    "        return Point(self.x-other.x, self.y-other.y, self.z-other.z)\n",
    "    \n",
    "    def __mul__(self, other):\n",
    "        if isinstance(other, Point):\n",
    "            return self.x*other.x + self.y*other.y + self.z*other.z\n",
    "        else:\n",
    "            return Point(self.x*other, self.y*other, self.z*other)\n",
    "    \n",
    "    def __div__(self, c):\n",
    "        return Point(self.x/c, self.y/c, self.z/c)\n",
    "    \n",
    "    def __mod__(self, c):\n",
    "        '''%\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __divmod__(self, c):\n",
    "        pass\n",
    "    \n",
    "    def __pow__(self, c):\n",
    "        '''**\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __pos__(self):\n",
    "        '''+(正号)运算符重载\n",
    "        '''\n",
    "        return Point(+self.x, +self.y, +self.z)\n",
    "    \n",
    "    def __neg__(self):\n",
    "        '''-(负号)运算符重载\n",
    "        '''\n",
    "        return Point(-self.x, -self.y, -self.z)\n",
    "    \n",
    "    def __iadd__(self, other):\n",
    "        '''+=运算符重载\n",
    "        '''\n",
    "        self.x += other.x\n",
    "        self.y += other.y\n",
    "        self.z += other.z\n",
    "        return self\n",
    "    \n",
    "    def __truediv__(self, c):\n",
    "        pass\n",
    "    \n",
    "    def __floordiv__(self, c):\n",
    "        '''//\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __invert__(self):\n",
    "        '''~取反\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __or__(self, other):\n",
    "        '''|\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __and__(self, other):\n",
    "        '''&\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __xor__(self, other):\n",
    "        '''^\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __lshift__(self, n):\n",
    "        '''<<\n",
    "        '''\n",
    "        pass\n",
    "\n",
    "    #序列\n",
    "    def __len__(self):\n",
    "        return 3\n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        if index == 0:\n",
    "            return self.x\n",
    "        elif index == 1:\n",
    "            return self.y\n",
    "        elif index == 2:\n",
    "            return self.z\n",
    "        else:\n",
    "            return None\n",
    "    \n",
    "    def __setitem__(self, index, value):\n",
    "        if index == 0:\n",
    "            self.x = value\n",
    "        elif index == 1:\n",
    "            self.y = value\n",
    "        elif index == 2:\n",
    "            self.z = value\n",
    "        else:\n",
    "            return None\n",
    "    \n",
    "    def __delitem__(self, index):\n",
    "        pass\n",
    "    \n",
    "    def __iter__(self):\n",
    "        '''for in\n",
    "        '''\n",
    "        pass\n",
    "    \n",
    "    def __getslice__(self, start, end):\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "(<class 'object'>,)\n",
      "<class '__main__.Point'>\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(issubclass(Point, object))\n",
    "print(Point.__bases__) #查看基类们\n",
    "p = Point(1,2,3)\n",
    "print(p.__class__) #属于哪个类\n",
    "print(isinstance(p, Point))\n",
    "print(hasattr(p, 'z'))\n",
    "print(4 in p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Atom(Point): #继承自Point\n",
    "    def __init__(self, x=0, y=0, z=0, e=''):\n",
    "        super(Atom, self).__init__(x, y, z)\n",
    "        self.elem = e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有该属性\n",
      "None\n",
      "<class '__main__.Point'>\n"
     ]
    }
   ],
   "source": [
    "a = Point(5)\n",
    "a.__class__ = Atom\n",
    "print(a.elem)\n",
    "print(a.__class__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "静态方法&类方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "操作符重载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\t8\t11\n"
     ]
    }
   ],
   "source": [
    "q = Point(4,6,8)\n",
    "print(p+q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多重继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C2\n",
      "C3\n",
      "C1\n"
     ]
    }
   ],
   "source": [
    "class C1(object):\n",
    "    def f(self):\n",
    "        print('C1')    \n",
    "\n",
    "class C2(C1):\n",
    "    def f(self):\n",
    "        print('C2') \n",
    "        super(C2, self).f() \n",
    "\n",
    "class C3(C1):\n",
    "    def f(self):\n",
    "        print('C3')\n",
    "        super(C3, self).f()\n",
    "        \n",
    "class C4(C2,C3):\n",
    "    pass\n",
    "\n",
    "d = C4()\n",
    "d.f()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
